package leetcode_二叉树;

import general_class.TreeNode;

/**
 * 二叉树的最大深度
 * 算法：递归、深度优先搜索
 *
 * @author origin
 */
public class _104 {


    public static void main(String[] args) {
        TreeNode r = new TreeNode(3, new TreeNode(9, null, null),
                new TreeNode(20, new TreeNode(15, null, null), new TreeNode(7, null, null)));
        System.out.println(new _104().maxDepth_recursion(r));
    }

    int ans;

    public int maxDepth(TreeNode root) {
        dfs(root, 0);
        return ans;
    }

    public void dfs(TreeNode root, int tem) {
        if (root == null) {
            ans = Math.max(ans, tem);
            return;
        }
        tem += 1;
        dfs(root.left, tem);
        dfs(root.right, tem);
    }

    public int maxDepth_recursion(TreeNode root) {
        if (root == null) return 0;
        return Math.max(maxDepth_recursion(root.left), maxDepth_recursion(root.right)) + 1;
    }

}
